<?php
require_once 'log/Logger.php';
$mId = $_POST["mId"];
$host_cnname = $_POST["host_cnname"];
$visit_cnname = $_POST["visit_cnname"];
$starttime = $_POST["starttime"];
//$companyodds = $_POST["companyodds"];

Logger::getLogger()->debug('odds enter');

//Logger::getLogger()->debug(file_get_contents("php://input"));
$POSTX = array();
$splitdata = split('&',file_get_contents("php://input"));
foreach($splitdata as $data)
{
    $sdata = split('=',$data);
    $POSTX[$sdata[0]]=$sdata[1];
}
$companyodds = $POSTX['companyodds'];
Logger::getLogger()->debug($companyodds);
//mId,host_cnname,visit_cnname,starttime,companyodds
$conn = mysql_connect('ltfll.gicp.net','qqxj','qqxj');
if(!$conn)
{
    Logger::getLogger()->debug('connect fail!'.mysql_error());
    echo "connect fail!".mysql_error()."\n";
}
mysql_select_db('match',$conn);

$day = date("Ymd",strtotime($starttime));
$sql = "insert into {$day}_matchdata (mId,host_cnname,visit_cnname,starttime) values ('{$mId}','{$host_cnname}','{$visit_cnname}','{$starttime}');";


$createtablesql =  "CREATE TABLE IF NOT EXISTS`{$day}_matchdata` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
  `mId` int(11) NOT NULL,
  `host_cnname` varchar(50) DEFAULT NULL,
  `visit_cnname` varchar(50) DEFAULT NULL,
  `host_trname` varchar(50) DEFAULT NULL,
  `visit_trname` varchar(50) DEFAULT NULL,
  `host_enname` varchar(50) DEFAULT NULL,
  `visit_enname` varchar(50) DEFAULT NULL,
  `starttime` datetime DEFAULT NULL,
  `matchresult` varchar(10) DEFAULT NULL,
  `league_cnname` varchar(50) DEFAULT NULL,
  `league_enname` varchar(50) DEFAULT NULL,
  `league_trname` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `mId` (`mId`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=gbk";
$rs = mysql_query($createtablesql, $conn);

mysql_query("SET NAMES 'GBK'");
$rs = mysql_query($sql, $conn);

if($rs)
{
    //Logger::getLogger()->debug("insert ok\n");
    //Logger::getLogger()->debug($sql."\n");
    echo "inser ok\n";
}
else
{
    Logger::getLogger()->debug('insert fail!'.mysql_error()."\n");
    Logger::getLogger()->debug($sql."\n");
    echo "insert fail!".mysql_error()."\n";
}
if(!file_exists("oddsdata"))
{
    mkdir("oddsdata");
}
if(!file_exists("oddsdata/{$day}oddsdata"))
{
    mkdir("oddsdata/{$day}oddsdata");
}
$md5 = md5($companyodds);
$datalen = strlen($companyodds);
Logger::getLogger()->debug("mId:{$mId},starttime:{$starttime},datalen:{$datalen},md5:{$md5}");
//file_put_contents("oddsdata/{$day}oddsdata/{$mId}.odds",$companyodds);
$file = fopen("oddsdata/{$day}oddsdata/{$mId}.odds",'wb');
fwrite($file,$companyodds);
fclose($file);
//file_put_contents("oddsdata/{$day}oddsdata/{$mId}.odds1",file_get_contents("php://input"));